/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.applis.demo.actions;

import opiam.admin.applis.demo.beans.Group;
import opiam.admin.applis.demo.forms.GroupForm;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.struts.actions.SecureAction;
import opiam.admin.faare.struts.utils.SessionContext;

import org.apache.log4j.Logger;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * This actions allows to remove a selected set of users from a group.
 */
public class GroupRemoveUsersAction extends SecureAction
{
    /** Logger de log4j. */
    private static Logger _logger = Logger.getLogger(GroupRemoveUsersAction.class);

    /**
     * Struts Action method.
     * See SecureAction
     *
     * @param mapping Struts mapping data.
     * @param actionForm Input form.
     * @param request HTTP request.
     * @param httpServletResponse HTTP response.
     *
     * @return forward to success or to error
     *
     * @throws java.io.IOException see Action.execute()
     * @throws javax.servlet.ServletException see Action.execute()
     */
    @Override
	public ActionForward secureExecute(
        ActionMapping mapping,
        ActionForm actionForm,
        HttpServletRequest request,
        HttpServletResponse httpServletResponse)
        throws IOException, ServletException
    {
    	
        SessionContext sessionContext = null;
        UserContext userContext = null;
        ActionMessages errors = new ActionMessages();
        try
		{
        	// Gets the session.
        	HttpSession session = request.getSession();
        	// Gets the session context.
        	sessionContext = SessionContext.getInstance(session);
        	// Gets the user context.
        	userContext = sessionContext.getUserContext();
        	// Gets the form
        	GroupForm gf = (GroupForm)actionForm;
        	
        	// Gets the request parameters
        	String[] elements = request.getParameterValues("memberindex");
        	
        	if (elements == null)
        	{
        		return (mapping.findForward("success"));
        	}
        	
        	Group group = gf.getGroup();
        	List lmembers = new ArrayList();
        	lmembers.addAll(group.getUniqueMember());
        	
        	for (int i = 0; i < elements.length; i++)
        	{
        		lmembers.remove(Integer.parseInt(elements[i]));
        	}
        	group.setUniqueMember(lmembers);
        	gf.setGroup(group);
        	
            return (mapping.findForward("success"));
        }
        catch (Exception se)
        {
            _logger.debug("Trace", se);
            _logger.error(se.getMessage());
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.service.unknown"));
            saveErrors(request, errors);

            return (mapping.findForward("error"));
        }
    }
}
